﻿using Newtonsoft.Json;
using PMSWenLvTing.BLL;
using PMSWenLvTing.Model;
using PMSWenLvTing.Model.JiuDian;
using PMSWenLvTing.Model.TongJi;
using PMSWenLvTing.Model.WenLvTing;
using PMSWenLvTing.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace PMSWenLvTing.Controllers
{
    public class WenLvTingController : BaseWLTController
    {
        MainBLL bllm = new MainBLL();
        TongJiBLL blltj = new TongJiBLL();

        //数据中心
        public ActionResult WLTDataCenter()
        {
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            List<t_area> lisArea = Self_GetMyArea(modelyhcc.aid_json, null);
            lisArea = lisArea.Where(kkm => kkm.level == 2).ToList();

            ViewBag.lisarea = JsonConvert.SerializeObject(lisArea);
            ViewBag.lisaid = JsonConvert.SerializeObject(lisArea.Select(kkm => kkm.areaId).ToList());
            ViewBag.lisbbtype = JsonConvert.SerializeObject(PublicStaticObject.lisBBType);

            return View();
        }

        //酒店管理
        public ActionResult WLTJDGuanLiView()
        {
            return View();
        }

        //工作管理
        public ActionResult WLTGongZuoGuanLiView()
        {
            return View();
        }

        //报表发布
        public ActionResult BBFaBuView()
        {
            List<DTO_Tree> listree = Self_GetTree();
            ViewBag.lisjd = JsonConvert.SerializeObject(listree);
            return View();
        }

        //账户管理
        public ActionResult WLTZhangHuGuanLiView()
        {
            return View();
        }

        //账户管理--添加子账号--编辑子账号
        public ActionResult WLTZhangHuShow()
        {
            #region 存放方法变量
            //图片验证码，调用同一个发送短信的方法时验证用
            Session["checkcode"] = "0000";

            string yhwltid_str = Request["yhwltid"];
            int yhwltid = -1;

            string leixing_str = Request["leixing"];
            int leixing = -1;
            #endregion

            if (int.TryParse(leixing_str, out leixing) == false)
            {
                //默认是子账号
                leixing = 3;
            }


            if (int.TryParse(yhwltid_str, out yhwltid) == false)
            {
                #region 添加账号
                DTO_yonghu_wenlvting modelyh_res = new DTO_yonghu_wenlvting();
                modelyh_res.aid_json = new List<int>();
                //界面传值
                ViewBag.sub_title = "新建账号";
                modelyh_res.leixing = leixing;
                if (leixing == 1 || leixing == 2)
                {
                    modelyh_res.quanxian = 1;
                }
                else
                {
                    modelyh_res.quanxian = 2;
                }
                ViewBag.modelyh = JsonConvert.SerializeObject(modelyh_res);
                List<t_area> lisarea = bllm.Get_t_area_hainan();
                ViewBag.lisarea = JsonConvert.SerializeObject(lisarea.Where(kkm => kkm.level == 2).OrderBy(kkm => kkm.areaCode).ToList());
                #endregion
            }
            else
            {
                #region 修改账号
                List<yonghu_wenlvting> lisyhwlt = bllm.Get_yonghu_wenlvting(string.Format("id={0}", yhwltid));
                if (lisyhwlt == null || lisyhwlt.Count <= 0)
                {
                    return JavaScript("<script>alert('无相关用户数据，请重新选择');history.back();</script>");
                }
                List<t_area> lisarea = bllm.Get_t_area_hainan();
                List<DTO_yonghu_wenlvting> lisyhwlt_dto = ToDTO.Self_ToYHWLTShow(lisyhwlt, lisarea);
                //界面传值
                ViewBag.sub_title = "编辑账号";
                ViewBag.modelyh = JsonConvert.SerializeObject(lisyhwlt_dto.First());
                ViewBag.lisarea = JsonConvert.SerializeObject(lisarea.Where(kkm => kkm.level == 2).OrderBy(kkm => kkm.areaCode).ToList());
                #endregion
            }
            return View();
        }

        //退出
        public ActionResult ExitP()
        {
            Session.Clear();
            return RedirectToAction("login", "login");
        }

        //获取待审核消息数量
        public ActionResult GetMsgInfoCount()
        {
            yonghu_wenlvting modelyhcc = GetCurrentUser();

            #region 待审核酒店数量
            //获取自己的管辖区域
            List<t_area> lisArea = Self_GetMyArea(modelyhcc.aid_json, null);
            if (lisArea.Count <= 0)
            {
                return Content("0");
            }
            string aid_str = string.Join(",", lisArea.Select(kkm => kkm.areaId));

            //获取酒店审核消息
            int totalCount_jd = bllm.Get_JiuDianCount(string.Format("shenhe_zhuangtai=0 and aid in ({0})", aid_str));
            #endregion

            #region 待审核报表数量
            //获取我的酒店信息
            List<jiudian> lisjd = Self_GetMyJiuDian(modelyhcc.aid_json, null);
            if (lisjd == null || lisjd.Count <= 0)
            {
                return Content(totalCount_jd.ToString());
            }
            string jdid_str = string.Join(",", lisjd.Select(kkm => kkm.id));

            //获取待审核的报表消息
            int totalCount_bb = bllm.Get_BBZhuangTaiCount(string.Format("zt_tianbao=1 and zt_shenhe=0 AND isdel=FALSE AND jdid in ({0})", jdid_str));
            return Content((totalCount_jd + totalCount_bb).ToString());
            #endregion
        }

        //获取待审核消息明细，前50条消息
        public ActionResult GetMsgInfoDetail()
        {
            #region 获取我的酒店信息
            yonghu_wenlvting modelyhcc = GetCurrentUser();

            List<jiudian> lisjd = Self_GetMyJiuDian(modelyhcc.aid_json, null);
            if (lisjd == null || lisjd.Count <= 0)
            {
                return Json(new List<DTO_WLTMsg>() { });
            }
            #endregion

            #region 获取待审核的酒店明细数据

            List<jiudian> lisjd_shenhe = lisjd.Where(kkm => kkm.shenhe_zhuangtai == 0).ToList();
            List<DTO_WLTMsg> lismsg_jd = ToDTO.Self_JDToWLTMsg(lisjd_shenhe);
            #endregion

            #region 获取待审核报表数据
            List<DTO_WLTMsg> lismsg_bb = new List<DTO_WLTMsg>();

            //获取报表状态数据
            List<baobiao_zhuangtai> liszt = new List<baobiao_zhuangtai>();
            liszt = bllm.Get_baobiao_zhuangtai(string.Format("zt_tianbao=1 and zt_shenhe=0 AND isdel=FALSE AND jdid in ({0})", string.Join(",", lisjd.Select(kkm => kkm.id))));
            if (liszt == null || liszt.Count <= 0)
            {
                return Json(lismsg_jd);
            }
            List<baobiao_fabu> lisfb = bllm.Get_baobiao_fabu(string.Format("id in ({0})", string.Join(",", liszt.Select(kkm => kkm.bbid))));
            if (lisfb == null)
            {
                lisfb = new List<baobiao_fabu>();
            }
            //转换数据
            lismsg_bb = ToDTO.Self_BBToWLTMsg(lisjd, lisfb, liszt);
            #endregion

            //合并数据
            List<DTO_WLTMsg> lismsg_all = new List<DTO_WLTMsg>();
            lismsg_all.AddRange(lismsg_jd);
            lismsg_all.AddRange(lismsg_bb);
            return Json(lismsg_all);
        }

        //获取酒店统计数据 数据中心--酒店信息
        public ActionResult GetTongJiJDInfo()
        {
            #region 方法变量，数据校验
            string dts_str = Request["dts"];
            string dte_str = Request["dte"];
            string lisaid_str = Request["lisaid"];
            int bbtype = int.Parse(Request["bbtype"]);
            List<int> lisaid = new List<int>();
            DateTime dts = DateTime.Now;
            DateTime dte = DateTime.Now;
            bool has_dts = true;
            bool has_dte = true;

            has_dts = DateTime.TryParse(dts_str, out dts) ? true : false;
            has_dte = DateTime.TryParse(dte_str, out dte) ? true : false;
            lisaid = JsonConvert.DeserializeObject<List<int>>(lisaid_str);

            DTO_TJ_JDInfo modelRes = new DTO_TJ_JDInfo();
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            #endregion

            #region 获取该用户所能管辖的选中酒店id
            List<int> lisaid_ok = Self_CompareAID(lisaid, null, modelyhcc.aid_json);
            #endregion

            #region 获取数据
            //获取酒店数据
            List<jiudian> lisjd = Self_GetMyJiuDian(null, lisaid_ok);
            if (lisjd == null)
            {
                return Json(modelRes);
            }
            else
            {
                lisjd = lisjd.Where(kkm => kkm.shenhe_zhuangtai <= 1).ToList();
                if (lisjd == null)
                {
                    lisjd = new List<jiudian>();
                }
            }

            //获取月报统计数据            
            string jdid_str = string.Join(",", lisjd.Select(kkm => kkm.id));
            string strWhere_yb = "";
            if (string.IsNullOrEmpty(jdid_str))
            {
                //没有酒店的时候就什么都查不出来，而不是什么都查
                strWhere_yb = string.Format(" 1=2 ");
            }
            else
            {
                strWhere_yb = string.Format(" jdid in ({0})  ", jdid_str);
            }
            if (has_dts)
            {
                strWhere_yb += string.Format("and bb_riqi>='{0}' ", dts_str);
            }
            if (has_dte)
            {
                strWhere_yb += string.Format("and bb_riqi<='{0}' ", dte_str);
            }
            DTO_YueBao_TJ modelYBTJ = blltj.Get_YueBao_TJ(strWhere_yb);

            #endregion

            #region 开始分析数据
            modelRes.n_jingji_jd = lisjd.Count(kkm => kkm.jiage_leixing_huafen == "经济型酒店");
            modelRes.n_keren_jingnei = modelYBTJ.jiedai_renshu_jingnei;
            modelRes.n_keren_jingwai = modelYBTJ.jiedai_renshu_jingwai;
            modelRes.n_mingsu_jd = lisjd.Count(kkm => kkm.jingying_leixing_huafen == "民宿类酒店");
            modelRes.n_reg = lisjd.Count;
            modelRes.n_room = lisjd.Sum(kkm => kkm.kefang_zongshu);
            modelRes.n_star_jd = lisjd.Count(kkm => kkm.is_xingji);
            #endregion


            return Json(modelRes);

        }

        //获取酒店统计数据 数据中心--工作督导
        public ActionResult GetTongJiBBZhuangTai()
        {
            #region 方法变量，数据校验
            string dts_str = Request["dts"];
            string dte_str = Request["dte"];
            string lisaid_str = Request["lisaid"];
            int bbtype = int.Parse(Request["bbtype"]);
            List<int> lisaid = new List<int>();
            DateTime dts = DateTime.Now;
            DateTime dte = DateTime.Now;
            bool has_dts = true;
            bool has_dte = true;

            has_dts = DateTime.TryParse(dts_str, out dts) ? true : false;
            has_dte = DateTime.TryParse(dte_str, out dte) ? true : false;
            lisaid = JsonConvert.DeserializeObject<List<int>>(lisaid_str);

            DTO_BBZT_TJ modelRes = new DTO_BBZT_TJ();
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            #endregion

            #region 获取该用户所能管辖的选中酒店id
            List<int> lisaid_ok = Self_CompareAID(lisaid, null, modelyhcc.aid_json);
            #endregion

            #region 获取数据
            //获取酒店数据
            List<jiudian> lisjd = Self_GetMyJiuDian(null, lisaid_ok);
            if (lisjd == null)
            {
                return Json(modelRes);
            }
            else
            {
                lisjd = lisjd.Where(kkm => kkm.shenhe_zhuangtai <= 1).ToList();
                if (lisjd == null)
                {
                    lisjd = new List<jiudian>();
                }
            }

            //获取报表状态统计数据            
            List<int> lisjdid = lisjd.Select(kkm => kkm.id).ToList();
            DateTime? dtstart = null;
            DateTime? dtend = null;
            if (has_dts)
            {
                dtstart = dts;
            }
            else
            {
                dtstart = null;
            }
            if (has_dte)
            {
                dtend = dte;
            }
            else
            {
                dtend = null;
            }
            modelRes = blltj.Get_BBZhuangTai_TJ(lisjdid, dtstart, dtend, bbtype);


            #endregion




            return Json(modelRes);

        }

        //获取酒店数据，用于展示
        public ActionResult GetJDInfoP()
        {
            #region 方法变量
            string pageIndex_str = Request["pageIndex"];
            string pageSize_str = Request["pageSize"];
            string txtsearch = Request["txtsearch"];

            int pageIndex = 1;
            int pageSize = 10;
            if (int.TryParse(pageIndex_str, out pageIndex) == false)
            {
                pageIndex = 1;
            }
            if (int.TryParse(pageSize_str, out pageSize) == false)
            {
                pageSize = 10;
            }
            #endregion

            #region 酒店数据
            DTO_JDShow_WLTAll modelRes = new DTO_JDShow_WLTAll();
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            List<jiudian> lisjd = Self_GetMyJiuDian(modelyhcc.aid_json, null);
            if (lisjd == null)
            {
                return Json(modelRes);
            }
            else
            {
                lisjd = lisjd.Where(kkm => kkm.shenhe_zhuangtai <= 1).ToList();
                if (lisjd == null)
                {
                    lisjd = new List<jiudian>();
                }
            }
            #endregion



            #region 分析数据
            List<t_area> lisArea = Self_GetMyArea(modelyhcc.aid_json, null);
            List<jiudian> lisjd_page = new List<jiudian>();
            if (string.IsNullOrEmpty(txtsearch) || string.IsNullOrWhiteSpace(txtsearch))
            {

            }
            else
            {
                foreach (jiudian item in lisjd)
                {
                    if (item.jd_name.Contains(txtsearch))
                    {
                        lisjd_page.Add(item);
                    }
                    else if (item.jingying_leixing_huafen.Contains(txtsearch))
                    {
                        lisjd_page.Add(item);
                    }
                    else if (item.danwei_fuzeren.Contains(txtsearch))
                    {
                        lisjd_page.Add(item);
                    }
                    else if (item.dianhua.Contains(txtsearch))
                    {
                        lisjd_page.Add(item);
                    }
                }
                lisjd = lisjd_page;
            }
            int pageStart = (lisjd.Count - 1) >= (pageIndex - 1) * pageSize ? (pageIndex - 1) * pageSize : 0;
            int pageCount = (pageStart + pageSize) <= lisjd.Count ? pageSize : lisjd.Count - pageStart;
            lisjd_page = lisjd.GetRange(pageStart, pageCount);
            List<DTO_JDShow_WLT> lisshow = ToDTO.Self_ToJDShowWLT(lisjd_page, lisArea);
            modelRes.pageIndex = pageIndex;
            modelRes.pageSize = pageSize;
            modelRes.lisjd = lisshow;
            string redirectParam = string.Format("GetJDInfoP(1");
            modelRes.pageHtml = LaomaPagerHelper.ShowPageNavigate(redirectParam, pageSize, pageIndex, lisjd.Count);
            #endregion

            return Json(modelRes);

        }

        //获取报表状态信息
        public ActionResult GetZTInfoP()
        {
            #region 存放方法变量
            //bb=baobiao报表
            string pageIndex_str = Request["pageIndex"];
            string pageSize_str = Request["pageSize"];
            string shenheid_str = Request["shenheid"];
            string txtsearch = Request["txtsearch"];
            string bblx_str = Request["bblx"];
            string bbdate1_str = Request["bbdate1"];
            string bbdate2_str = Request["bbdate2"];

            DateTime bbdate1 = DateTime.Now;
            DateTime bbdate2 = DateTime.Now;
            int shenheid = -1;
            int bblx = -1;
            int pageIndex = 1;
            int pageSize = 10;
            bool bbdate1_ok = true;
            bool bbdate2_ok = true;

            //初始化结果数据
            DTO_ZTShow_WLTAll modelRes = new DTO_ZTShow_WLTAll();
            modelRes.pageHtml = "";
            modelRes.liszt = new List<DTO_ZTShow_WLT>();
            modelRes.pageIndex = 1;
            modelRes.pageSize = 10;
            #endregion

            #region 检查数据有效性

            if (int.TryParse(pageIndex_str, out pageIndex) == false)
            {
                pageIndex = 1;
            }
            if (int.TryParse(pageSize_str, out pageSize) == false)
            {
                pageSize = 10;
            }
            if (int.TryParse(shenheid_str, out shenheid) == false)
            {
                return Json(modelRes);
            }
            if (int.TryParse(bblx_str, out bblx) == false)
            {
                return Json(modelRes);
            }
            if (bblx == 1)
            {
                bbdate1_str += "-01";
                bbdate2_str += "-01";
            }
            else if (bblx == 2)
            {
                bbdate1_str += "-01-01";
                bbdate2_str += "-01-01";
            }
            if (DateTime.TryParse(bbdate1_str, out bbdate1) == false)
            {
                bbdate1_ok = false;
            }
            if (DateTime.TryParse(bbdate2_str, out bbdate2) == false)
            {
                bbdate2_ok = false;
            }
            #endregion

            #region 获取数据
            List<string> lisWhere = new List<string>();

            #region 酒店数据
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            List<jiudian> lisjd = Self_GetMyJiuDian(modelyhcc.aid_json, null);
            if (lisjd == null)
            {
                return Json(modelRes);
            }
            else
            {
                lisjd = lisjd.Where(kkm => kkm.shenhe_zhuangtai <= 1).ToList();
                if (lisjd == null)
                {
                    lisjd = new List<jiudian>();
                }
            }
            List<jiudian> lisjd_sel = new List<jiudian>();
            if (string.IsNullOrEmpty(txtsearch) || string.IsNullOrWhiteSpace(txtsearch))
            {
                lisjd_sel = lisjd;
            }
            else
            {
                foreach (jiudian item in lisjd)
                {
                    if (item.jd_name.Contains(txtsearch))
                    {
                        lisjd_sel.Add(item);
                    }
                    else if (item.jingying_leixing_huafen.Contains(txtsearch))
                    {
                        lisjd_sel.Add(item);
                    }
                    else if (item.danwei_fuzeren.Contains(txtsearch))
                    {
                        lisjd_sel.Add(item);
                    }
                    else if (item.dianhua.Contains(txtsearch))
                    {
                        lisjd_sel.Add(item);
                    }
                }
            }
            if (lisjd_sel == null || lisjd_sel.Count <= 0)
            {
                //没有符合条件的酒店，则直接返回
                return Json(modelRes);
            }

            lisWhere.Add(string.Format("jdid in ({0})", string.Join(",", lisjd_sel.Select(kkm => kkm.id))));

            #endregion

            #region 报表类型
            if (bblx != -1)
            {
                lisWhere.Add(string.Format("leixing={0}", bblx));
            }
            #endregion

            #region 审核状态
            if (shenheid != -1)
            {
                lisWhere.Add(string.Format("zt_shenhe={0}", shenheid));
            }

            #endregion

            #region 报表日期
            if (bbdate1_ok == false && bbdate2_ok == false)
            {
                //所有时间范围
            }
            else if (bbdate1_ok == true && bbdate2_ok == true)
            {
                //有限时间范围
                lisWhere.Add(string.Format("bb_riqi>='{0}' and bb_riqi<='{1}'", bbdate1.ToString("yyyy-MM-dd"), bbdate2.ToString("yyyy-MM-dd")));
            }
            else if (bbdate1_ok == true && bbdate2_ok == false)
            {
                //有开始时间
                lisWhere.Add(string.Format("bb_riqi>='{0}'", bbdate1.ToString("yyyy-MM-dd")));
            }
            else if (bbdate1_ok == false && bbdate2_ok == true)
            {
                //有结束时间
                lisWhere.Add(string.Format("bb_riqi<='{0}'", bbdate2.ToString("yyyy-MM-dd")));
            }
            #endregion

            #region 已删除
            lisWhere.Add("isdel=false");
            #endregion



            #region 获取总条数
            string strWhere = string.Join(" and ", lisWhere);
            int totalCount = bllm.Get_BBZhuangTaiCount(strWhere);
            if (totalCount <= 0)
            {
                return Json(modelRes);
            }
            #endregion

            #region 获取报表状态数据
            if (lisWhere.Count <= 0)
            {
                strWhere = string.Format("1=1 limit {0},{1}", (pageIndex - 1) * pageSize, pageSize);
            }
            else
            {
                strWhere += string.Format(" limit {0},{1}", (pageIndex - 1) * pageSize, pageSize);
            }

            List<baobiao_zhuangtai> lisbbzt = bllm.Get_baobiao_zhuangtai(strWhere);

            #endregion

            #region 获取报表发布数据
            List<baobiao_fabu> lisbbfb = new List<baobiao_fabu>();
            lisbbfb = bllm.Get_baobiao_fabu(string.Format("id in ({0})", string.Join(",", lisbbzt.Select(kkm => kkm.bbid))));
            if (lisbbfb == null)
            {
                lisbbfb = new List<baobiao_fabu>();
            }
            #endregion

            #region 获取用户酒店数据
            List<yonghu_jiudian> lisyhjd = new List<yonghu_jiudian>();
            lisyhjd = bllm.Get_yonghu_jiudian(string.Format("id in ({0})", string.Join(",", lisbbzt.Select(kkm => kkm.yyjdid))));
            if (lisyhjd == null)
            {
                lisyhjd = new List<yonghu_jiudian>();
            }
            #endregion

            #region 获取用户文旅厅数据
            List<yonghu_wenlvting> lisyhwlt = new List<yonghu_wenlvting>();
            lisyhwlt = bllm.Get_yonghu_wenlvting(string.Format("id in ({0})", string.Join(",", lisbbzt.Select(kkm => kkm.yywltid))));
            if (lisyhwlt == null)
            {
                lisyhwlt = new List<yonghu_wenlvting>();
            }
            #endregion


            #endregion

            #region 分析数据
            List<DTO_ZTShow_WLT> lisshow = ToDTO.Self_ToZTShow_WLT(lisjd, lisbbfb, lisbbzt, lisyhjd, lisyhwlt);
            modelRes.pageIndex = pageIndex;
            modelRes.pageSize = pageSize;
            modelRes.liszt = lisshow;
            string redirectParam = string.Format("GetBaoBiaoP(1");
            modelRes.pageHtml = LaomaPagerHelper.ShowPageNavigate(redirectParam, pageSize, pageIndex, totalCount);
            return Json(modelRes);
            #endregion

        }

        //文旅厅报表展示界面
        public ActionResult WLTBaoBiaoShow()
        {
            #region 有效性检查
            string bbztid_str = Request["bbztid"];
            int bbztid = -1;
            if (int.TryParse(bbztid_str, out bbztid) == false)
            {
                return Content("无效报表，请刷新后重试");
            }
            List<baobiao_zhuangtai> lisbbzt = bllm.Get_baobiao_zhuangtai(string.Format("id={0}", bbztid));
            if (lisbbzt == null || lisbbzt.Count <= 0)
            {
                return Content("无效报表，请刷新后重试");
            }
            //检查这个酒店是否属于这个用户的
            yonghu_wenlvting modelyycc = GetCurrentUser();
            List<jiudian> lisjd = Self_GetMyJiuDian(modelyycc.aid_json, null);
            if (lisjd.Exists(kkm => kkm.id == lisbbzt.First().jdid) == false)
            {
                return Content("抱歉，您无权查看");
            }
            //获取报表发布数据
            List<baobiao_fabu> lisbbfb = bllm.Get_baobiao_fabu(string.Format("id={0}", lisbbzt.First().bbid));
            #endregion

            #region 返回界面
            ViewBag.jd_name = lisjd.Find(kkm => kkm.id == lisbbzt.First().jdid).jd_name;
            ViewBag.bb_name = lisbbfb.First().bb_name;
            if (lisbbfb.First().leixing == 1)
            {
                #region 月报
                //判断是否存在，存在则展示数据，不存在则不展示
                List<yuebao> lisyb = bllm.Get_yuebao(string.Format("bbid={0} and jdid={1} order by tianbao_shijian desc limit 0,1", lisbbfb.First().id, lisbbzt.First().jdid));
                if (lisyb == null || lisyb.Count <= 0)
                {
                    #region 填写
                    DTO_yuebao modelyb = new DTO_yuebao();
                    modelyb.bbid = lisbbfb.First().id;
                    modelyb.jdid = lisbbzt.First().jdid;
                    modelyb.shenhe_zhuangtai = 0;
                    modelyb.yyjdid = -1;
                    modelyb.yywltid = -1;
                    modelyb.fabu_shijian_jiezhi = lisbbfb.First().tianbao_jieshu.ToString("yyyy-MM-dd");
                    modelyb.fabu_shijian_kaishi = lisbbfb.First().tianbao_kaishi.ToString("yyyy-MM-dd");
                    modelyb.baobiao_mingcheng = lisbbfb.First().bb_name;
                    modelyb.has_zanting = false;
                    modelyb.bb_riqi = lisbbfb.First().bb_riqi.ToString("yyyy-MM-dd");

                    ViewBag.modelYB = JsonConvert.SerializeObject(modelyb);
                    ViewBag.showData = 1;
                    #endregion
                }
                else
                {
                    #region 展示
                    ViewBag.modelYB = JsonConvert.SerializeObject(lisyb.First());
                    ViewBag.showData = 0;
                    #endregion
                }
                return View("WLTYueBaoTianBao");
                #endregion
            }
            else if (lisbbfb.First().leixing == 2)
            {
                #region 年报
                //判断是否存在，存在则展示数据，不存在则不展示
                List<nianbao> lisnb = bllm.Get_nianbao(string.Format("bbid={0} and jdid={1} order by tianbao_shijian desc limit 0,1", lisbbfb.First().id, lisbbzt.First().jdid));
                if (lisnb == null || lisnb.Count <= 0)
                {
                    #region 填写
                    DTO_nianbao modelnb = new DTO_nianbao();
                    modelnb.bbid = lisbbfb.First().id;
                    modelnb.jdid = lisbbzt.First().jdid;
                    modelnb.shenhe_zhuangtai = 0;
                    modelnb.yyjdid = -1;
                    modelnb.yywltid = -1;
                    modelnb.fabu_shijian_jiezhi = lisbbfb.First().tianbao_jieshu.ToString("yyyy-MM-dd");
                    modelnb.fabu_shijian_kaishi = lisbbfb.First().tianbao_kaishi.ToString("yyyy-MM-dd");
                    modelnb.baobiao_mingcheng = lisbbfb.First().bb_name;
                    modelnb.has_zanting = false;
                    modelnb.bb_riqi = lisbbfb.First().bb_riqi.ToString("yyyy-MM-dd");

                    ViewBag.modelNB = JsonConvert.SerializeObject(modelnb);
                    ViewBag.showData = 1;
                    #endregion
                }
                else
                {
                    #region 展示
                    ViewBag.modelNB = JsonConvert.SerializeObject(lisnb.First());
                    ViewBag.showData = 0;
                    #endregion
                }
                return View("WLTNianBaoTianBao");
                #endregion
            }
            else
            {
                return Content("无此类报表");
            }
            #endregion
        }

        //修改报表状态
        public ActionResult ModifyBBZhuangTaiP()
        {
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            int bbtype = int.Parse(Request["bbtype"]);
            int bbid = int.Parse(Request["bbid"]);
            int zt = int.Parse(Request["zt"]);
            string beizhu = Request["beizhu"];
            int res = -1;
            yonghu_wenlvting modelyycc = GetCurrentUser();
            if (bbtype == 1)
            {
                #region 月报
                List<yuebao> lisbb = bllm.Get_yuebao(string.Format("id={0}", bbid));
                if (lisbb == null || lisbb.Count <= 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "无报表数据", false, ""));
                }
                res = bllm.ModifyBBZhuangTai(bbtype, lisbb.First().bbid, lisbb.First().jdid, zt, beizhu, modelyycc.id);
                #endregion
            }
            else if (bbtype == 2)
            {
                #region 年报
                List<nianbao> lisbb = bllm.Get_nianbao(string.Format("id={0}", bbid));
                if (lisbb == null || lisbb.Count <= 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "无报表数据", false, ""));
                }
                res = bllm.ModifyBBZhuangTai(bbtype, lisbb.First().bbid, lisbb.First().jdid, zt, beizhu, modelyycc.id);
                #endregion
            }
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "操作成功！", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "操作失败，请刷新后重试！", false, ""));
            }

        }

        //获取文旅厅用户信息
        public ActionResult WLTGetUserP()
        {
            List<yonghu_wenlvting> lisUser = new List<yonghu_wenlvting>();
            lisUser = bllm.Get_yonghu_wenlvting(string.Format("zhuangtai<3"));
            if (lisUser == null)
            {
                lisUser = new List<yonghu_wenlvting>();
            }
            List<t_area> lisarea = bllm.Get_t_area_hainan();
            List<DTO_yonghu_wenlvting> lisShow = new List<DTO_yonghu_wenlvting>();
            lisShow = ToDTO.Self_ToYHWLTShow(lisUser, lisarea);
            return Json(lisShow);
        }

        //处理文旅厅用户的状态信息
        public ActionResult WLTYHZhuangTaiP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 存放方法变量
            string yhwltid_str = Request["yhwltid"];
            string zt_str = Request["zt"];
            int yhwltid = -1;
            int zt = 1;
            #endregion

            #region 有效性判断
            if (int.TryParse(yhwltid_str, out yhwltid) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效用户", false, ""));
            }
            if (int.TryParse(zt_str, out zt) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效操作", false, ""));
            }
            else if (zt >= 4 || zt <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效操作", false, ""));
            }

            yonghu_wenlvting modelyh = GetCurrentUser();
            if (modelyh.leixing == 3)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉！您无权操作", false, ""));
            }
            List<yonghu_wenlvting> lisyhwlt = bllm.Get_yonghu_wenlvting(string.Format("id={0}", yhwltid));
            if (lisyhwlt == null || lisyhwlt.Count <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "无效用户", false, ""));
            }
            else if (lisyhwlt.First().leixing == 1)
            {
                return Json(CommonHelper.Self_GetRes(-1, "禁止操作主账户", false, ""));
            }

            #endregion

            #region 操作数据库
            int res = bllm.Modify_yonghu_wenlvting_zt(yhwltid, zt);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "操作成功！", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "操作失败，请刷新重试！", false, ""));
            }
            #endregion



        }

        //处理文旅厅用户的基本信息 添加子账户,子管理员，修改账户
        public ActionResult WLTYHShowP()
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 检查权限
            yonghu_wenlvting modelcc = GetCurrentUser();
            
            #endregion

            #region 获取界面用户数据
            string modelyh_dto_str = Request["modelyh"];
            DTO_yonghu_wenlvting modelyh_dto = new DTO_yonghu_wenlvting();
            modelyh_dto = JsonConvert.DeserializeObject<DTO_yonghu_wenlvting>(modelyh_dto_str);
            #endregion

            #region 检查数据是否有效
            string dxyzm = Request["dxyzm"];
            if (string.IsNullOrEmpty(modelyh_dto.xingming) || string.IsNullOrWhiteSpace(modelyh_dto.xingming))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入姓名！", false, ""));
            }
            if (string.IsNullOrEmpty(modelyh_dto.zhanghao))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入电话号码！", false, ""));
            }
            else
            {
                if (Regex.IsMatch(modelyh_dto.zhanghao, @"^1[3456789]\d{9}$") == false)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "无效电话号码！", false, ""));
                }
                if (Session["dxphone"] != null && string.IsNullOrEmpty(Session["dxphone"].ToString()) == false && Self_CheckImgYZM("dxphone", modelyh_dto.zhanghao) == false)
                {
                    //更换了电话号码，需要重新发送短信
                    Session["dxyzm"] = "";
                    return Json(CommonHelper.Self_GetRes(-1, "请重新发送短信验证码！", false, ""));
                }
            }

            if (string.IsNullOrEmpty(dxyzm))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请输入短信验证码！", false, ""));
            }
            else
            {
                if (Self_CheckImgYZM("dxyzm", dxyzm) == false)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "短信验证码错误！", false, ""));
                }
            }
            #endregion
            if (modelyh_dto.id <= 0)
            {
                #region 添加子账号
                //账号是否存在
                string whereStr = " zhanghao='" + modelyh_dto.zhanghao + "' AND zhuangtai<=2 ";
                List<yonghu_wenlvting> lisyh_exist = bllm.Get_yonghu_wenlvting(whereStr);
                if (lisyh_exist != null && lisyh_exist.Count > 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "抱歉！当前账号已被占用，请更换账号后重试", false, ""));
                }

                //添加子账号数据
                yonghu_wenlvting modeladd = new yonghu_wenlvting();
                modeladd.aid_json = JsonConvert.SerializeObject(modelyh_dto.aid_json);
                modeladd.leixing = modelyh_dto.leixing;
                modeladd.quanxian = modelyh_dto.quanxian;
                modeladd.xingming = modelyh_dto.xingming;
                modeladd.zhanghao = modelyh_dto.zhanghao;
                modeladd.zhuangtai = 1;

                int res = bllm.Add_yonghu_wenlvting(modeladd);
                if (res > 0)
                {
                    //清理短信计时
                    Session["dxtime"] = null;
                    return Json(CommonHelper.Self_GetRes(1, "操作成功", false, ""));
                }
                else
                {
                    return Json(CommonHelper.Self_GetRes(-1, "操作失败！请刷新后重试", false, ""));
                }
                #endregion
            }
            else
            {
                #region 修改用户
                //账号是否存在
                string whereStr = " zhanghao='" + modelyh_dto.zhanghao + "' AND zhuangtai<=2 AND id<>" + modelyh_dto.id;
                List<yonghu_wenlvting> lisyh_exist = bllm.Get_yonghu_wenlvting(whereStr);
                if (lisyh_exist != null && lisyh_exist.Count > 0)
                {
                    return Json(CommonHelper.Self_GetRes(-1, "抱歉！当前账号已被占用，请更换账号后重试", false, ""));
                }

                //修改账号数据
                yonghu_wenlvting modelmodify = new yonghu_wenlvting();
                modelmodify.id = modelyh_dto.id;
                if (modelcc.id == modelmodify.id && modelcc.leixing == 1)
                {
                    //主账户的文旅厅数据不允许修改
                    modelmodify.leixing = modelcc.leixing;
                    modelmodify.quanxian = modelcc.quanxian;
                }
                else
                {
                    modelmodify.leixing = modelyh_dto.leixing;
                    modelmodify.quanxian = modelyh_dto.quanxian;
                }
                modelmodify.xingming = modelyh_dto.xingming;
                modelmodify.zhanghao = modelyh_dto.zhanghao;
                modelmodify.aid_json = JsonConvert.SerializeObject(modelyh_dto.aid_json);

                int res = bllm.Modify_yonghu_wenlvting_info(modelmodify);
                if (res > 0)
                {
                    //清理短信计时
                    Session["dxtime"] = null;
                    return Json(CommonHelper.Self_GetRes(1, "操作成功", false, ""));
                }
                else
                {
                    return Json(CommonHelper.Self_GetRes(-1, "操作失败！请刷新后重试", false, ""));
                }

                #endregion
            }

        }

        //获取报表发布数据，用于展示
        public ActionResult GetBBFaBuP()
        {
            #region 方法变量
            string pageIndex_str = Request["pageIndex"];
            string pageSize_str = Request["pageSize"];

            int pageIndex = 1;
            int pageSize = 10;
            if (int.TryParse(pageIndex_str, out pageIndex) == false)
            {
                pageIndex = 1;
            }
            if (int.TryParse(pageSize_str, out pageSize) == false)
            {
                pageSize = 10;
            }
            #endregion

            #region 分页数据
            int totalCount = bllm.Get_BBFaBuCount("isdel=false");
            #endregion


            #region 报表发布数据
            DTO_BBShow_WLTAll modelRes = new DTO_BBShow_WLTAll();
            List<baobiao_fabu> lisbbfb = bllm.Get_baobiao_fabu(string.Format("isdel=false order by fabu_shijian desc limit {0},{1}", (pageIndex - 1) * pageSize, pageSize));
            if (lisbbfb == null)
            {
                return Json(modelRes);
            }
            #endregion



            #region 分析数据
            List<DTO_BBShow_WLT> lisshow = ToDTO.Self_ToBBShowWLT(lisbbfb);
            modelRes.pageIndex = pageIndex;
            modelRes.pageSize = pageSize;
            modelRes.lisfb = lisshow;
            string redirectParam = string.Format("GetBBFaBuP(1");
            modelRes.pageHtml = LaomaPagerHelper.ShowPageNavigate(redirectParam, pageSize, pageIndex, totalCount);
            #endregion

            return Json(modelRes);

        }

        //发布报表处理方法
        public ActionResult BBFaBuP(DTO_BBShow_WLT modelreq)
        {
            #region 权限判断
            if (Self_CheckQX() == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "抱歉，您没有操作权限！", false, ""));
            }
            #endregion

            #region 检查数据有效性
            if (string.IsNullOrEmpty(modelreq.bb_name) || string.IsNullOrWhiteSpace(modelreq.bb_name))
            {
                return Json(CommonHelper.Self_GetRes(-1, "请填写报表名称", false, ""));
            }
            DateTime bb_riqi = DateTime.Now;
            if (modelreq.leixing == 1)
            {
                modelreq.bb_riqi_str += "-01";
            }
            else if (modelreq.leixing == 2)
            {
                modelreq.bb_riqi_str += "-01-01";
            }
            if (DateTime.TryParse(modelreq.bb_riqi_str, out bb_riqi) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "请填写正确的报表日期", false, ""));
            }
            DateTime tianbao_kaishi = DateTime.Now;
            DateTime tianbao_jieshu = DateTime.Now;
            if (DateTime.TryParse(modelreq.tianbao_kaishi_str, out tianbao_kaishi) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "请填写正确的开始时间", false, ""));
            }
            if (DateTime.TryParse(modelreq.tianbao_jieshu_str, out tianbao_jieshu) == false)
            {
                return Json(CommonHelper.Self_GetRes(-1, "请填写正确的结束时间", false, ""));
            }
            List<int> lisjdid = JsonConvert.DeserializeObject<List<int>>(modelreq.aid_json);
            if (lisjdid == null || lisjdid.Count <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "请选择发布范围", false, ""));
            }
            lisjdid = lisjdid.Where(kkm => kkm < 10000000).ToList();
            if (lisjdid == null || lisjdid.Count <= 0)
            {
                return Json(CommonHelper.Self_GetRes(-1, "请选择发布范围", false, ""));
            }

            #endregion

            #region 拼接发布数据
            yonghu_wenlvting modelyhcc = GetCurrentUser();
            baobiao_fabu modeladd = new baobiao_fabu();
            modeladd.bb_name = modelreq.bb_name;
            modeladd.bb_riqi = bb_riqi;
            modeladd.fabu_ren = modelyhcc.xingming;
            modeladd.fabu_shijian = DateTime.Now;
            modeladd.id = new BillCodeHelper().Get_BBFBID();
            modeladd.isdel = false;
            modeladd.is_duanxin = modelreq.is_duanxin;
            modeladd.leixing = modelreq.leixing;
            modeladd.tianbao_jieshu = tianbao_jieshu;
            modeladd.tianbao_kaishi = tianbao_kaishi;
            modeladd.yywltid = modelyhcc.id;

            #endregion

            #region 修改数据库
            int res = bllm.Add_baobiao_fabu(modeladd, lisjdid);
            if (res > 0)
            {
                return Json(CommonHelper.Self_GetRes(1, "操作成功！", false, ""));
            }
            else
            {
                return Json(CommonHelper.Self_GetRes(-1, "操作失败！请重试", false, ""));
            }
            #endregion
        }


        #region 自定义方法区

        /// <summary>
        /// 获取发布范围的树形结构
        /// </summary>
        /// <returns></returns>
        public List<DTO_Tree> Self_GetTree()
        {
            List<t_area> lisarea = new List<t_area>();
            lisarea = bllm.Get_t_area_hainan().OrderBy(kkm => kkm.areaCode).ToList();
            List<jiudian> lisjd = new List<jiudian>();
            lisjd = bllm.Get_jiudian(null, false);
            if (lisjd == null)
            {
                lisjd = new List<jiudian>();
            }
            int pre = 10000000;
            List<jiudian> lisjd_tmp = new List<jiudian>();
            DTO_Tree model_one = new DTO_Tree();
            foreach (t_area item_one in lisarea)
            {
                if (item_one.level == 1)
                {
                    #region 一级
                    model_one.id = item_one.areaId + pre;
                    model_one.label = item_one.areaName;
                    model_one.children = new List<DTO_Tree>();
                    foreach (t_area item_two in lisarea)
                    {
                        #region 二级
                        if (item_two.parentId == item_one.areaId)
                        {
                            DTO_Tree model_two = new DTO_Tree();
                            model_two.id = item_two.areaId + pre;
                            model_two.label = item_two.areaName;
                            model_two.children = new List<DTO_Tree>();
                            #region 三级
                            if (lisarea.Exists(kkm => kkm.parentId == item_two.areaId))
                            {
                                foreach (t_area item_three in lisarea)
                                {
                                    if (item_three.parentId == item_two.areaId)
                                    {
                                        DTO_Tree model_three = new DTO_Tree();
                                        model_three.id = item_three.areaId + pre;
                                        model_three.label = item_three.areaName;
                                        model_three.children = new List<DTO_Tree>();
                                        #region 酒店
                                        lisjd_tmp = lisjd.Where(kkm => kkm.aid == item_three.areaId).ToList();
                                        if (lisjd_tmp != null)
                                        {
                                            foreach (jiudian jdmodel in lisjd_tmp)
                                            {
                                                model_three.children.Add(Self_GetTree(jdmodel));
                                            }
                                        }
                                        #endregion
                                        model_two.children.Add(model_three);
                                    }
                                }
                            }
                            else
                            {
                                #region 酒店
                                lisjd_tmp = lisjd.Where(kkm => kkm.aid == item_two.areaId).ToList();
                                if (lisjd_tmp != null)
                                {
                                    foreach (jiudian jdmodel in lisjd_tmp)
                                    {
                                        model_two.children.Add(Self_GetTree(jdmodel));
                                    }
                                }
                                #endregion
                            }
                            #endregion
                            model_one.children.Add(model_two);
                        }
                        #endregion

                    }
                    #endregion
                    break;
                }

            }

            return new List<DTO_Tree>() { model_one };
        }

        /// <summary>
        /// 获取当前账号所管辖的区域
        /// 可以传入原始的json格式aid，也可以传入序列化好的aid，二选一
        /// </summary>
        /// <param name="json_areaid">区域id，Json字符串</param>
        /// <param name="lisAreaid">区域id，list</param>
        /// <returns>对应的区域</returns>
        private List<t_area> Self_GetMyArea(string json_areaid, List<int> lisAreaid)
        {
            if (lisAreaid == null)
            {
                lisAreaid = JsonConvert.DeserializeObject<List<int>>(json_areaid);
            }
            if (lisAreaid == null || lisAreaid.Count <= 0)
            {
                return new List<t_area>();
            }
            List<t_area> lisArea = bllm.Get_t_area_hainan();
            List<t_area> lisRes = new List<t_area>();
            foreach (int aid in lisAreaid)
            {
                foreach (t_area item in lisArea)
                {
                    if (item.areaId == aid || item.parentId == aid)
                    {
                        lisRes.Add(item);
                    }
                }
            }

            return lisRes.OrderBy(kkm => kkm.areaCode).ToList();
        }

        /// <summary>
        /// 获取当前账号所管辖的酒店
        /// 可以传入原始的json格式aid，也可以传入序列化好的aid，二选一
        /// </summary>
        /// <param name="json_areaid"></param>
        /// <param name="lisAreaid"></param>
        /// <returns></returns>
        private List<jiudian> Self_GetMyJiuDian(string json_areaid, List<int> lisAreaid)
        {
            List<t_area> lisArea = Self_GetMyArea(json_areaid, lisAreaid);
            if (lisArea == null || lisArea.Count <= 0)
            {
                return new List<jiudian>();
            }
            //根据区域ID获取酒店数据 内存中获取
            return bllm.Get_jiudian_aid(lisArea.Select(kkm => kkm.areaId).ToList(), false);

        }

        /// <summary>
        /// 比较选中的aid和用户能够管辖的aid，取交集
        /// </summary>
        /// <param name="lisaid_sel">选中的aid</param>
        /// <param name="lisaid_yy">用户可以管辖的aid</param>
        /// <param name="yy_json_areaid">用户可以管辖的aid</param>
        /// <returns>取交集</returns>
        private List<int> Self_CompareAID(List<int> lisaid_sel, List<int> lisaid_yy, string yy_json_areaid)
        {
            if (lisaid_yy == null)
            {
                lisaid_yy = JsonConvert.DeserializeObject<List<int>>(yy_json_areaid);
            }
            //获取选中的所有下级
            lisaid_sel = bllm.Self_GetAIDSub_HaiNan(lisaid_sel);
            lisaid_yy = bllm.Self_GetAIDSub_HaiNan(lisaid_yy);
            List<int> lisRes = new List<int>();
            lisRes = lisaid_yy.Intersect(lisaid_sel).ToList();
            return lisRes;

        }

        /// <summary>
        /// 检查验证码是否一致
        /// </summary>
        /// <param name="sessionName">session中存放的名字</param>
        /// <param name="yzm"></param>
        /// <returns></returns>
        public bool Self_CheckImgYZM(string sessionName, string yzm)
        {
            string sess_yzm = Session[sessionName] as string;
            if (string.IsNullOrEmpty(sess_yzm) || string.IsNullOrEmpty(yzm))
            {
                return false;
            }
            if (sess_yzm.ToLower() != yzm.ToLower())
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取一个dto_tree
        /// </summary>
        /// <param name="jdmodel"></param>
        /// <returns></returns>
        private DTO_Tree Self_GetTree(jiudian jdmodel)
        {
            DTO_Tree model = new DTO_Tree();
            model.id = jdmodel.id;
            model.label = jdmodel.jd_name;
            model.children = null;
            return model;
        }

        /// <summary>
        /// 判断当前用户是否有操作权限
        /// </summary>
        /// <returns></returns>
        private bool Self_CheckQX()
        {
            if (GetCurrentUser().quanxian == 2)
            {
                return false;
            }
            return true;
        }


        #endregion

    }
}
